package leetcode.每日一题;

import org.junit.Test;

import java.util.Arrays;

/**
 * @author ：zsy
 * @date ：Created 2022/12/16 1:55 PM
 * @description：
 */
public class 构成特定和需要添加的最少元素 {
    @Test
    public void test() {
        System.out.println(new Solution().minElements(new int[]{1, -1, 1}, 3, -4));
    }

    /*class Solution {
        // sum 溢出整型
        public int minElements(int[] nums, int limit, int goal) {
            int sum = Arrays.stream(nums).sum();
            if (sum == goal) return 0;
            sum = Math.abs(sum - goal);
            int ans = sum / limit;
            ans += sum % limit == 0 ? 0 : 1;
            return ans;
        }
    }*/

    class Solution {
        public int minElements(int[] nums, int limit, int goal) {
            long sum = 0;
            for (int x : nums) {
                sum += x;
            }
            long diff = Math.abs(sum - goal);
            return (int)((diff + limit - 1) / limit);
        }
    }
}
